export default class FormPropsController {
  constructor() {
    this._rules = {}
  }
  /**
   * 给字段添加验证
   * @param {string} field 
   * @param {object} rule 
   */
  addFieldRule(field, rule, position) {
    if (!this._rules[field]) {
      this._rules[field] = []
    }
    if (position !== undefined) {
      this._rules[field][position] = rule
    } else {
      if (!this._rules[field].length) {
        this._rules[field][1] = rule
      } else {
        this._rules[field].push(rule)
      }
    }
  }
  /**
   *  移除字段验证
   * @param {string} field 
   * @param {number} position 
   */
  removeFieldRule(field, position) {
    if (position === 0) {
      this._rules[field] && (this._rules[field][0] = null)
      return
    }
    this._rules[field] ?.splice(position, 1)
  }
  /**
   * 获取 field 对应的校验规则
   * @param {String} field 
   * @returns 
   */
  getRulesByField(field) {
    return this._rules[field]
  }
  /**
   * 获取表单所有的验证规则
   * @returns 
   */
  getRules() {
    return this._rules
  }
}